import { host, auth_name } from './config.js'
class HTTP {
  constructor(){
    this.host = host
    this.auth_name = auth_name
  }
  
  request(url='', methods='GET', data = {}, header_type='json'){
    return new Promise((resolve,reject)=>{
      wx.request({
        url: this.host + url,
        data,
        methods: methods.toUpperCase(),
        header : {
          'content-type': HTTP.header_type_check(header_type),
          [this.auth_name] : HTTP.get_token(this.auth_name)
        },
        success:res=>{
          resolve(res)
        },
        fail:err=>{
          reject(err)
        }
      })
    })
  }
  
  //设置请求头类型
  static header_type_check(header_type='json'){
    if (header_type.toUpperCase === 'JSON')  return 'application/json'
    else return 'application/x-www-form-urlencoded'
  }

  //获取令牌
  static get_token(key='token'){
    return wx.getStorageSync(key) ? wx.getStorageSync(key) : ''
  }
}


export{
  HTTP
}